
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<style>
	#addLineDiv .form-group select, #addLineDiv .form-group input {
		width: calc(100vw / 2 - 300px);
	}
	.peices{
		width: calc(100vw / 2 - 350px) !important;
	}
	.row {
		margin-right: 0px;
		margin-left: 0px;
	}
	.hr-line-dashed{
		margin:5px 0;
		border-top: 0px dashed #e7eaec;
	}
	.btnOk{
		background: #2c74fa;
		color:#fff;
	}
</style>
<div class="modal" id="addLineDiv" tabindex="-1" role="dialog" aria-hidden="true">

	<div class="modal-dialog" role="document">

		<div class="modal-content animated bounceInRight" style="width: 1000px;">

			<div class="modal-header">
				<h4 class="modal-title redragon-modal-title">入库行编辑</h4>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>

			<div class="modal-body" style="padding-bottom: 20px;">
				<%-- 导入提示信息框 --%>
			    <c:if test="${hint!=null&&hint!=''}">
			   		<jsp:include page="../../common/alert/alert.jsp">
			   			<jsp:param value="${hint}" name="alertType"/>
			   			<jsp:param value="${alertMessage}" name="alertMessage"/>
			   		</jsp:include>
			    </c:if>
			
				<form id="lineForm">
					<div id="materialCodeDiv">
						<div class="form-group row">
							<div class="col-sm-6">
								<label class="col-form-label">物料编码</label>
								<div class="cinput-group">
									<input id="materialCode" type="text" class="form-control" value="${requestScope.invInputLine.materialCode}" readonly="readonly">
								</div>
							</div>
							<div class="col-sm-6">
								<label class="col-form-label">物料名称</label>
								<div class="col-sm-4 input-group">
									<input id="materialName" type="text" class="form-control" value="${requestScope.invInputLine.materialName}" readonly="readonly">
								</div>
							</div>

						</div>
						<div class="hr-line-dashed"></div>
					</div>
					<div id="materialStandardDiv">
						<div class="form-group row">
							<div class="col-sm-6">
								<label class="col-form-label">规格型号</label>
								<div class="col-sm-4 input-group">
									<input id="materialStandard" type="text" class="form-control" value="${requestScope.invInputLine.materialStandard}" readonly="readonly">
								</div>
							</div>
							<div class="col-sm-6">
								<label class="col-form-label"><span class="text-danger">*</span>单价</label>
								<div class="col-sm-4 input-group">
									<input id="price"  type="text" class="form-control peices" value="${requestScope.invInputLine.price}" readonly="readonly">
									<span class="input-group-addon">(元)</span>
								</div>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
					</div>
					<div id="quantityDiv">
						<div class="form-group row">
							<div class="col-sm-6">
								<label class="col-form-label">数量</label>
								<div class="col-sm-4 input-group">
									<input id="quantity" type="text" class="form-control peices" value="${requestScope.invInputLine.quantity}" readonly="readonly">
									<span class="input-group-addon" >(${requestScope.typeMap[requestScope.invInputLine.unit]})</span>
									<input id="unit" value="${requestScope.invInputLine.unit}" style="display: none;">
								</div>
							</div>
							<div class="col-sm-6">
								<label class="col-form-label"><span class="text-danger">*</span><span class="inputQuantityTitle">入库数量</span></label>
								<div class="col-sm-4 input-group">
									<input id="inputQuantity" name="inputQuantity" type="text" class="form-control" value="${requestScope.invInputLine.inputQuantity}">
								</div>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
					</div>
					<div id="attachedDiv" style="display: none;">
						<div class="form-group row">
							<div class="col-sm-12">
								<label class="col-form-label">赠送说明</label>
								<div class="col-sm-10 input-group">
									<input id="attachedMemo" name="attachedMemo" type="text" class="form-control" value="${requestScope.invInputLine.attachedMemo}">
								</div>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
					</div>
					<div id="discountDiv" style="display: none;">
						<div class="form-group row">
							<label class="col-form-label">折扣</label>
							<div class="col-sm-4">
								<input id="discount" name="discount" type="text" class="form-control" value="${requestScope.invInputLine.discount}">
							</div>
							<label class="col-form-label">折扣原因</label>
							<div class="col-sm-4">
								<input id="discountMemo" name="discountMemo" type="text" class="form-control" value="${requestScope.invInputLine.discountMemo}">
							</div>
						</div>
						<div class="hr-line-dashed"></div>
					</div>
					<div id="isReportDiv" style="display: none;">
						<div class="form-group row">
							<div class="col-sm-6">
								<label class="col-form-label"><span class="text-danger">*</span>是否有质检报告</label>
								<div class="col-sm-4 input-group">
									<select class="form-control" name="isReport" id="isReport">
										<option value="" selected="selected">请选择...</option>
										<c:forEach items="${requestScope.isReportMap}" var="isReport">
											<option value="${isReport.key}">${isReport.value}</option>
										</c:forEach>
									</select>
								</div>
							</div>
							<div class="col-sm-6">
								<label class="col-form-label"><span class="text-danger">*</span><span>库存方式</span></label>
								<div class="col-sm-4 input-group">
									<select class="form-control" name="invMeans" id="invMeans">
										<option value="" selected="selected">请选择...</option>
										<c:forEach items="${requestScope.invMeansMap}" var="invMeans">
											<option value="${invMeans.key}">${invMeans.value}</option>
										</c:forEach>
									</select>
								</div>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
					</div>
					<div class="form-group row">
						<div class="col-sm-6">
							<label class=" col-form-label" id="memoLabel">入库摘要</label>
							<div class="col-sm-10">
								<input id="inputLineMemo" name="memo" type="text" class="form-control" value="${requestScope.invInputLine.memo}">
							</div>
						</div>
					</div>
					<div class="hr-line-dashed"></div>

					<div class="form-group row m-b-none">
						<div class="col-sm-12  text-right">
							<button class="btn btnBack" type="button"
								data-dismiss="modal">返回</button>
							&nbsp;
							<button
								class="btn btnOk"
								data-style="expand-right">
								确定
							</button>
						</div>
					</div>
					
					<input type="hidden" id="inputHeadCode" name="inputHeadCode" value="${param.inputHeadCode}"> 
					<input type="hidden" id="inputLineId" name="inputLineId" value="${requestScope.invInputLine.inputLineId}"> 
					<input type="hidden" id="inputLineCode" name="inputLineCode" value="${requestScope.invInputLine.inputLineCode}"> 
					<input type="hidden" id="inputSourceLineCode" name="inputSourceLineCode" value="${requestScope.invInputLine.inputSourceLineCode}"> 
					<input type="hidden" id="createdDate" name="createdDate" value="${requestScope.invInputLine.createdDate}"> 
					<input type="hidden" id="createdBy" name="createdBy" value="${requestScope.invInputLine.createdBy}">
				</form>
			</div>

		</div>

	</div>

</div>


<script>
	$(document).ready(function() {
		debugger
        //设置isReport默认值
        if("${requestScope.invInputLine.isReport}"!=""){
            $("#isReport").val("${requestScope.invInputLine.isReport}");
        }
        //设置invMeans默认值
        if("${requestScope.invInputLine.invMeans}"!=""){
            $("#invMeans").val("${requestScope.invInputLine.invMeans}");
        }
        if("${requestScope.invInputLine.quantity}" < "${requestScope.invInputLine.inputQuantity}"){
            $("#attachedDiv").css('display','block');
        }

        //初始化折扣
        if("${requestScope.approveStatus}"!="UNSUBMIT"){
            $("#discountDiv").css('display','block');
        }

		//初始化入库类型效果
		if($("#inputType").val()=="RETURN"){
			$(".redragon-modal-title").text("退货行编辑");
			$(".inputQuantityTitle").text("退货数量");
			$("#memoLabel").text("退货原因");
			$("#inputQuantity").css("color","red");

		}else{
			$(".redragon-modal-title").text("入库行编辑");
			$(".inputQuantityTitle").text("入库数量");
			$("#inputQuantity").css("color","black");
            $("#isReportDiv").css('display','block');
		}
		
		//初始化领料效果
		if($("#inputType").val()!="PROD_RETURN"){
			$("#prodNumberSpan").remove();
		}
		

			$("#price").prop("readonly", true);
			$("#quantity").prop("readonly", true);
			//$("#materialStandardDiv").show();
			//$("#materialNameDiv").show();
			//$("#materialCodeDiv").show();
			$("#materialCodeOtherDiv").remove();
			$("#unitDiv").remove();

		//初始化物料选择框
		function initMaterialCode(){
			if($("#inputType").val()=="PROD_RETURN"){
				//如果是退料则设置任务需求数量
				$("#prodNumber").text($("#materialCode").find("option:checked").attr("prodQuantity"));
				$("#inputSourceLineCode").val($("#materialCode").find("option:checked").attr("prodLine"));
			}
		}
		
		
		
		//表单提交
		var l = $('.ladda-button-demo').ladda();

		l.click(function() {
			$("#lineForm").valid();
			//l.ladda('stop');
		});

		$("#lineForm").validate({
			rules : {
				inputQuantity : {
					required : true,
					gtZero : true,
				},
			},
			submitHandler: function(form) {
				var submitFlag = "Y";
				//杂项入库类型验证
				if($("#materialCode").val()==""){
					redragonJS.alert("物料选择必填");
					submitFlag = "N";
					return false;
				}

				if($("#price").val()==""){
					redragonJS.alert("单价必填");
					submitFlag = "N";
					return false;
				}
				if($("#unit").val()==""){
					redragonJS.alert("单位选择必填");
					submitFlag = "N";
					return false;
				}
				if( $("#isReportDiv").css('display') == "block"){
                    if($("#isReport").val()==""){
                        redragonJS.alert("请选择是否有质检报告");
                        submitFlag = "N";
                        return false;
                    }
                    if($("#invMeans").val()==""){
                        redragonJS.alert("请选择库存方式");
                        submitFlag = "N";
                        return false;
                    }
				}
				//表单提交
				if(submitFlag=="Y"){
					l.ladda('start');
					editLine();
				}
		    }
		});
		
	});

	$("#inputQuantity").blur(function(){
	    var quantity = $("#quantity").val();
	    debugger
		if(parseFloat(quantity) < parseFloat($(this).val())){
		    $("#attachedDiv").css('display','block');
		}else{
            $("#attachedDiv").css('display','none');
		}

    });
	//异步编辑付款行
	function editLine(){
		redragonJS.loading("ibox-content1");
		debugger
		//获取字段
		var unit = "";
		if($("#unit").length>0){
			unit = $("#unit").val();
		}
		
		var price = "";
		if($("#inputSourceType").val()!="PO"&&$("#price").length>0){
			price = $("#price").val();
		}
		
		//计算行金额
		var amount = "";
		if(price!=""&&$.isNumeric(price)){
			amount = redragonJS.numberMulti(price, $("#inputQuantity").val());
		}
		$.ajax({
			type: "post",
			url: "web/invInputLine/editInvInputLine",
			data: {"quantity": $("#quantity").val(),"inputQuantity": $("#inputQuantity").val(), "memo": $("#inputLineMemo").val(), "inputHeadCode": $("#inputHeadCode").val(), "inputLineId": $("#inputLineId").val(), "headInputType": $("#inputType").val(),
				   "inputLineCode": $("#inputLineCode").val(),"attachedMemo": $("#attachedMemo").val(), "discount": $("#discount").val(), "discountMemo": $("#discountMemo").val(), "inputSourceLineCode": $("#inputSourceLineCode").val(), "materialCode": $("#materialCode").val(), "unit": unit, "price": price, "amount": amount, "isReport": $("#isReport").val(), "invMeans": $("#invMeans").val(),
				   "createdDate": $("#createdDate").val(), "createdBy": $("#createdBy").val(), "version": $("#version").val()},
			async: false,
			dataType: "html",
			cache: false,
			success: function(data){
				redragonJS.removeLoading("ibox-content1");
				$('#addLineDiv').modal('hide');
				$('.ladda-button-demo').ladda('stop');
				
				var json = JSON.parse(data);
				if(json.result=="success"){
					getLineTab($("#inputHeadCode").val());
				}else{
					redragonJS.alert("编辑行信息错误");
				}
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
</script>
